package com.gitee.flwwsg.rpc.transport.command;

import lombok.Getter;
import lombok.Setter;

import java.nio.charset.StandardCharsets;

@Getter
@Setter
public class ResponseHeader extends Header {
    private int code;
    private String error;

    //构造函数
    public ResponseHeader(int type, int version, int requestId, Throwable throwable) {
        this(type, version, requestId, Code.UNKNOWN_ERROR.getCode(), throwable.getMessage());
    }

    public ResponseHeader(int type, int version, int requestId) {
        this(type, version, requestId, Code.SUCCESS.getCode(), null);
    }

    public ResponseHeader(int type, int version, int requestId, int code, String error) {
        super(type, version, requestId);
        this.code = code;
        this.error = error;
    }

    //长度
    @Override
    public int length() {
        //多一个错误信息长度
        return Integer.BYTES * 5 + (error == null ? 0 : error.getBytes(StandardCharsets.UTF_8).length);
    }

}
